package com.jia.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDatasource() {
        return new DruidDataSource();
    }

    // 后台监控功能
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        // 后台需要有人登录
        HashMap<String, String> map = new HashMap<>();
        map.put("loginUsername","admin");  // key数固定的
        map.put("loginPassword","123456"); // key数固定的

        // 允许谁能访问
        map.put("allow","");

        // 静止谁能访问 map.put("jiajia","127.0.0.23");

        bean.setInitParameters(map);
        return bean;
    }

    @Bean
    public FilterRegistrationBean webStartFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        HashMap<String, String> map = new HashMap<>();

        // 这些东西不进行统计
        map.put("exclusions","*.js,*.css,/druid/*");

        bean.setInitParameters(map);
        return bean;
    }
}
